package com.sz.stock.service;

import com.sz.stock.mapper.StockBuyOrSaleMapper;
import com.sz.stock.model.Entrust;
import com.sz.stock.model.StockBuyOrSale;
import com.sz.stock.model.StockBuyOrSaleExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;
@Service
public class BuyOrSaleServiceImpl implements IBuyOrSaleService {
    @Autowired
    private StockBuyOrSaleMapper stockBuyOrSaleMapper;

    //查符合条件(一个)
    @Override
    public List<StockBuyOrSale> search(String code, String type, BigDecimal price) {
        StockBuyOrSaleExample example = new StockBuyOrSaleExample();
        StockBuyOrSaleExample.Criteria criteria = example.createCriteria();
        criteria.andStockCodeEqualTo(code);
        criteria.andStockBSTypeEqualTo(type);
        criteria.andStockBSPriceEqualTo(price);
        return stockBuyOrSaleMapper.selectByExample(example);
    }

    @Override
    public Integer addOrUpdate(Entrust entrust) {
        int i = 0;
        String timeSSS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Timestamp(System.currentTimeMillis()));
        List<StockBuyOrSale> list = search(entrust.getStockEntrustCode(), entrust.getStockEntrustOperate(), entrust.getStockEntrustMoney());
        if (list.size() != 0) {
            //存在
            StockBuyOrSale stockBuyOrSale = list.get(0);
            int quantity = stockBuyOrSale.getStockBSQuantity() + entrust.getStockEntrustNum();
            stockBuyOrSale.setStockBSQuantity(quantity);
            i = stockBuyOrSaleMapper.updateByPrimaryKeySelective(stockBuyOrSale);
        } else {
            //不存在，新建一个
            StockBuyOrSale stockBuyOrSale = new StockBuyOrSale(null, entrust.getStockEntrustCode(), entrust.getStockEntrustOperate(), entrust.getStockEntrustMoney(), entrust.getStockEntrustNum(), entrust.getStockEntrustStockid(), timeSSS);
            i = stockBuyOrSaleMapper.insertSelective(stockBuyOrSale);
        }
        return i;
    }

    @Override
    public Integer addOrUpdate1(Entrust entrust,Integer num) {
        if (num==0){
            return 0;
        }
        int i = 0;
        String timeSSS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Timestamp(System.currentTimeMillis()));
        List<StockBuyOrSale> list = search(entrust.getStockEntrustCode(), entrust.getStockEntrustOperate(), entrust.getStockEntrustMoney());
        if (list.size() != 0) {
            //存在
            StockBuyOrSale stockBuyOrSale = list.get(0);
            int quantity = stockBuyOrSale.getStockBSQuantity() + num;
            stockBuyOrSale.setStockBSQuantity(quantity);
            if(quantity<=0){
                //删除
                i = stockBuyOrSaleMapper.deleteByPrimaryKey(stockBuyOrSale.getStockBSId());
            }else{
                //修改
                i = stockBuyOrSaleMapper.updateByPrimaryKeySelective(stockBuyOrSale);
            }

        } else {
            //不存在，新建一个
            StockBuyOrSale stockBuyOrSale = new StockBuyOrSale(null, entrust.getStockEntrustCode(), entrust.getStockEntrustOperate(), entrust.getStockEntrustMoney(), num, entrust.getStockEntrustStockid(), timeSSS);
            i = stockBuyOrSaleMapper.insertSelective(stockBuyOrSale);
        }
        return i;
    }

    @Override
    public Integer deleteOne(String code, String type, BigDecimal price) {
        StockBuyOrSaleExample example = new StockBuyOrSaleExample();
        StockBuyOrSaleExample.Criteria criteria = example.createCriteria();
        criteria.andStockCodeEqualTo(code);
        criteria.andStockBSTypeEqualTo(type);
        criteria.andStockBSPriceEqualTo(price);
        return stockBuyOrSaleMapper.deleteByExample(example);
    }


    @Override
    public List<StockBuyOrSale> getFive(String code, String type) {
        return stockBuyOrSaleMapper.selectFive(code,type);
    }

    @Override
    public List<StockBuyOrSale> getAll(String code, String type) {
        return stockBuyOrSaleMapper.selectAll(code,type);
    }

    @Override
    public Integer updateBos(String code, String type, BigDecimal price, Integer num) {
        List<StockBuyOrSale> list = search(code, type, price);
        int i=0;
        if (list.size() != 0) {
            //存在
            StockBuyOrSale stockBuyOrSale = list.get(0);
            stockBuyOrSale.setStockBSQuantity(num);
            i=stockBuyOrSaleMapper.updateByPrimaryKeySelective(stockBuyOrSale);
        }
        return i;
    }


    @Override
    public Integer updateBos(StockBuyOrSale stockBuyOrSale) {
        int i = stockBuyOrSaleMapper.updateByPrimaryKeySelective(stockBuyOrSale);
        return i;
    }

    @Override
    public List<StockBuyOrSale> get110(String type) {
        StockBuyOrSaleExample example = new StockBuyOrSaleExample();
        StockBuyOrSaleExample.Criteria criteria = example.createCriteria();
        criteria.andStockBSTypeEqualTo(type);
        List<StockBuyOrSale> stockBuyOrSales = stockBuyOrSaleMapper.selectByExample(example);
        return stockBuyOrSales;
    }

}
